<?php
/**
 * jishupu-cms
 * @Author:技术铺<jishupu@qq.com>
 * @Date: 2021/11/11
 */

namespace App\Http\Controllers;

use App\Filters\DictionaryFilter;
use App\Http\Requests\DictionaryRequest;
use App\Http\Resources\DictionaryCollection;
use App\Http\Resources\DictionaryResource;
use App\Http\Resources\DictionaryTypeResource;
use App\Models\Dictionary;
use App\Services\DictionaryService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * 数据字典模块
 * Class DictionaryController
 * @package App\Http\Controllers
 */
class DictionaryController extends Controller
{

    /**
     * 列表
     * @param DictionaryFilter $filter
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(DictionaryFilter $filter)
    {
        $paginate = Dictionary::filter($filter)->paginate($this->pageSize());
        $result = DictionaryCollection::make($paginate)->except(['dictionaryItems'])->toArray(null);
        return $this->success('', $result);
    }

    /**
     * 添加
     * @param DictionaryRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function store(DictionaryRequest $request)
    {
        $data = $request->validate('store');
        $dictionary = new Dictionary($data);
        if (!$dictionary->save()) return $this->errorStored();
        return $this->successStored();
    }

    /**
     * 查看
     * @param Dictionary $dictionary
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(Dictionary $dictionary)
    {
        $result = DictionaryResource::make($dictionary)->except(['dictionaryItems', 'updated_at', 'created_at'])->toArray(null);
        return $this->success('', $result);
    }

    /**
     * 修改
     * @param DictionaryRequest $request
     * @param Dictionary $dictionary
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function update(DictionaryRequest $request, Dictionary $dictionary)
    {
        $data = $request->validate('update');
        if (!$dictionary->update($data)) return $this->errorUpdated();
        return $this->successUpdated();
    }

    /**
     * 删除
     * @param $id // 1或者'1,2'
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy($id)
    {
        DB::beginTransaction();
        try {
            $between = explode(',', $id);
            Dictionary::destroy($between);
            DB::commit();
            return $this->successDestoryed();
        } catch (\Exception $e) {
            DB::rollback();
            return $this->exception($e->getMessage())->errorDestoryed();
        }
    }

    /**
     * 更新状态
     * @param DictionaryRequest $request
     * @param Dictionary $dictionary
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     * @throws \Illuminate\Validation\ValidationException
     */
    public function updateStatus(DictionaryRequest $request, Dictionary $dictionary)
    {
        $data = $request->validate('updateStatus');
        if (!$dictionary->update($data)) return $this->errorUpdated();
        return $this->successUpdated();
    }

    /**
     * 获取数据字典公共配置
     * @return \Illuminate\Http\JsonResponse
     */
    public function commonConfig()
    {
        return $this->success('', ['items' => DictionaryService::getAllList()]);
    }
}
